﻿Main = {
    init: function() {
        Sys.Browser.WebKit = {};
        if (navigator.userAgent.indexOf('WebKit/') > -1) {
            Sys.Browser.agent = Sys.Browser.WebKit;
            Sys.Browser.version = parseFloat(navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);

            Sys.Browser.name = 'WebKit';
        }
        // button in action bar
        //$("button, input:submit, a", ".action_bar").button();
        $(".button_submit").button();

        // Tabs
        //enable cookie for tabs
        $("#windowtabs").tabs({
            cookie: { expires: 1 },
            add: function(event, ui) {
                //select newely opened tab
                $(this).tabs('select', ui.index);
                /*var url = $.data(ui.tab, 'load.tabs');
                if (url) {
                var page = '<iframe src="' + url + '" width="100%" height="100%"></iframe>';
                $(ui.panel).html(page);
                return false;
                }*/
                //load function to close tab
                Main.removetab($(this), ui.index);
                return false;
            },
            show: function(event, ui) {
                //load function to close selected tabs
                Main.removetab($(this), ui.index);
            }
        });
        $(".addwindowtab").click(function() {
            var href = $(this).attr("href");
            var title = $(this).attr("title");
            $("#windowtabs").tabs('add', href, title + ' &nbsp;');
            return false;
        });

        $(".accordion").accordion({ autoHeight: false });
        $('.accordion-custom .ui-dialog-titlebar').click(function() {
            if ($($(this).children()[0]).attr('class') == 'groupWidget-max') {
                $($(this).children()[0]).attr('class', 'groupWidget-min');
            }
            else {
                $($(this).children()[0]).attr('class', 'groupWidget-max');
            }

            $(this).next().toggle('slow');
            return false;
        });

        Main.initPopup('system_popup1');
        Main.initPopup('system_popup2');
        Main.initPopup('system_popup3');
    },
    removetab: function(tabselector, index) {
        $(".removetab").click(function() {
            $(tabselector).tabs('remove', index);
        });
    },
    redirectPage: function(url) {
        window.location.href = url;
    },
    redirectWithTimeout: function(url, timeout) {
        setTimeout('Main.redirectPage("' + url + '")', timeout);
    },
    updateMasterMessage: function() {
        $('#spMasterMessage').text('');
    },
    clearMasterMessage: function() {
        //setTimeout('Main.updateMasterMessage()', 10000);
    },
    setMasterMessage: function(msg) {
        $('#spMasterMessage').text(msg);
        Main.clearMasterMessage();
    },
    integer_textbox_keypress: function(e) {
        var keynum;
        if (window.event) // IE
        {
            keynum = e.keyCode
        }
        else if (e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which
        }
        if (keynum != 8 && keynum != 0 && (keynum < 48 || keynum > 57)) {
            return false;
        }
    },
    double_textbox_keypress: function(e) {
        var keynum;
        if (window.event) // IE
        {
            keynum = e.keyCode
        }
        else if (e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which
        }
        if (keynum != 8 && keynum != 0 && keynum != 46 && (keynum < 48 || keynum > 57)) {
            return false;
        }
    },
    number_textbox_focus: function(id) {
        var value = $('#' + id).val() + "";
        while (value.indexOf(",") >= 0) {
            value = value.replace(",", "");
        }
        $('#' + id).val(value);
    },
    double_textbox_blur: function(id) {
        var formated_value = $().number_format($('#' + id).val(), { numberOfDecimals: 2, roundDecimal: 0 });
        $('#' + id).val(formated_value);
    },
    doubleCustomNumberOfDecimals_textbox_blur: function(id, intNumberOfDecimals) {
        var formated_value = $().number_format($('#' + id).val(), { numberOfDecimals: intNumberOfDecimals });
        $('#' + id).val(formated_value);
    },
    integer_textbox_blur: function(id) {
        var formated_value = $().number_format($('#' + id).val(), { numberOfDecimals: 0 });
        $('#' + id).val(formated_value);
    },
    initPopup: function(popupid) {
        $('#' + popupid).dialog({
            autoOpen: false,
            bgiframe: false,
            width: 500,
            height: 300,
            modal: true,
            buttons: {
                "X": function() {
                    $(this).dialog("close");
                }
            }
        });
        $("#" + popupid).parent().appendTo($("form:first"));
    },
    openPopup: function(popupid) {
        $('#' + popupid).dialog('open');
    }
}

/************* add-on *************************/
function disableTextBox(txtid) {
    $("#" + txtid).attr("disabled", "disabled");
}
function enableTextBox(txtid) {
    $("#" + txtid).removeAttr("disabled");
    $("#" + txtid).focus();
}
function CheckCapsLock(e) {
    kc = e.keyCode ? e.keyCode : e.which;
    sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
    if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk)) {
        OnCapslockOn();
    }
    else {
        OnCapslockOff();
    }
}

function OnCapslockOn() {
    document.getElementById('LoginForm_FailureText').innerHTML = 'Caps Lock is On. To prevent entering your password incorrectly, you should press Caps Lock to turn it off.';
}
function OnCapslockOff() {
    var elem = document.getElementById('LoginForm_FailureText');
    if (elem.innerHTML != '') { elem.innerHTML = ''; }
}

function get_datetime_string(Culture) {
    var currentTime = new Date()
    var hour = currentTime.getHours()
    var ap = "AM";
    if (hour > 11) { ap = "PM"; }
    if (hour > 12) { hour = hour - 12; }
    if (hour == 0) { hour = 12; }
    hour = (hour < 10 ? "0" : "") + hour;
    var minute = currentTime.getMinutes()
    minute = (minute < 10 ? "0" : "") + minute;
    var month = currentTime.getMonth() + 1
    if (Culture == 'vn') {
        switch (month) {
            case 1:
                month = "Th1";
                break;
            case 2:
                month = "Th2";
                break;
            case 3:
                month = "Th3";
                break;
            case 4:
                month = "Th4";
                break;
            case 5:
                month = "Th5";
                break;
            case 6:
                month = "Th6";
                break;
            case 7:
                month = "Th7";
                break;
            case 8:
                month = "Th8";
                break;
            case 9:
                month = "Th9";
                break;
            case 10:
                month = "Th10";
                break;
            case 11:
                month = "Th11";
                break;
            case 12:
                month = "Th12";
                break;
        }
    }
    else {
        switch (month) {
            case 1:
                month = "Jan";
                break;
            case 2:
                month = "Feb";
                break;
            case 3:
                month = "Mar";
                break;
            case 4:
                month = "Apr";
                break;
            case 5:
                month = "May";
                break;
            case 6:
                month = "Jun";
                break;
            case 7:
                month = "Jul";
                break;
            case 8:
                month = "Aug";
                break;
            case 9:
                month = "Sep";
                break;
            case 10:
                month = "Oct";
                break;
            case 11:
                month = "Nov";
                break;
            case 12:
                month = "Dec";
                break;
        }
    }
    var day = currentTime.getDate()
    day = (day < 10 ? "0" : "") + day;
    var year = currentTime.getFullYear()
    if (Culture == 'vn')
    { return (day + " " + month + " , " + year + " " + hour + ":" + minute + " " + ap); }
    return (month + " " + day + " , " + year + " " + hour + ":" + minute + " " + ap);
}

/********************************************/
function pageLoad(sender, args) {
    if (args.get_isPartialLoad()) {
        //rebind jquery after partial load
        Main.init();
    }
}
/* init */
$(document).ready(function() {
    Main.init();
});